package benny.ApkposStore.Activity;

import java.text.SimpleDateFormat;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import benny.ApkposStore.R;
import benny.ApkposStore.db.DBHelper;
import benny.ApkposStore.utils.ApkUntils;
import benny.ApkposStore.utils.Apkglobal;

//import com.tencent.elife.asyn.BaseManager;
//import com.tencent.elife.asyn.Command;
//import com.tencent.elife.asyn.DataResponse;

public class RoomoperateActivity extends Activity {

	private String room_name;
	private String room_id;
	private Button changeroom;
	private TextView roomselect;
	static Context mcx;
	private SimpleDateFormat dateformat;
	private Button closeroom;
	private SQLiteDatabase db;
	private int price_type;
	private Button addroom;
	private static String orderid;
	private static String tag;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub

		super.onCreate(savedInstanceState);
		ApkUntils.fullscreen(this);
		mcx=this;
		dateformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		setContentView(R.layout.roomoperate);
		Intent intent = getIntent();
		Bundle bundle = intent.getExtras();// 根据bundle的key得到对应的对象
		room_name = bundle.getString("room_name");
		room_id = bundle.getString("room_id");
		price_type= bundle.getInt("price_type");
		changeroom = (Button) findViewById(R.id.changeroombtn); // 取得组件
		addroom = (Button) findViewById(R.id.addroombtn); // 取得组件
		closeroom = (Button) findViewById(R.id.roomselectclose); // 取得组件
		roomselect = (TextView) findViewById(R.id.roomselecttxt); // 取得组件
		roomselect.setText("你选择的房台是：" + room_name);
		closeroom.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish(); // 关闭弹出窗口
			}
		});
	
		addroom.setOnClickListener(new OnClickListener() {
		

			@Override
			public void onClick(View v) {//换台
				String sql = "select ifnull(table_extent,'') as table_extent  from apkpos_orders_new where table_num="+room_id+"";
				Cursor cursor=null;
				cursor=rawQuery(db, mcx, sql);
				if (cursor.getCount()==0) {
					 Toast.makeText(RoomoperateActivity.this, "该台是空闲，无需搭台！", Toast.LENGTH_SHORT)
					 .show();
					 return ;
				}
				if (cursor.getCount()>=5) {
					Toast.makeText(RoomoperateActivity.this, "该台已经搭够5桌，无法搭台！", Toast.LENGTH_SHORT)
					.show();
					return ;
				}
				String vipstr="";
				String vipflag="";
				while (cursor.moveToNext()) {
					
					vipstr= vipstr+cursor.getString(cursor
							.getColumnIndex("table_extent"));
				}
				
				if (vipstr.indexOf("B")==-1) {
					vipflag="B";
				}
				else if (vipstr.indexOf("C")==-1) {
					vipflag="C";
				}
				else if (vipstr.indexOf("D")==-1) {
					vipflag="D";
				}
				else if (vipstr.indexOf("E")==-1) {
					vipflag="E";
				}
				else {
					vipflag="F";
				}
				cursor.close();
				String date = dateformat.format(new java.util.Date());
				SimpleDateFormat dateformatorder = new SimpleDateFormat(
						"yyyyMMdd");
				String dateorder = dateformatorder.format(new java.util.Date());
				orderid=ApkUntils.getOdno_var_new(RoomoperateActivity.this,dateorder);
				String orderint = ApkUntils.ReadSharedPreferences(RoomoperateActivity.this, "odnotable", "id");
				try {
					sql="insert into apkpos_orders_new(orderid,orderint,table_num,table_extent,people_num,dish_total,"
							+ "room_state,order_state,open_time,dic_rate,openuser)"
							+ " values ('"
							+ orderid
							+ "',"
							+ orderint
							+ ","
							+ room_id
							+ ",'"+vipflag+"',1,0,3,0,'"
							+ date
							+ "',100,'"
							+ Apkglobal.getrealname() + "')";
					execSQL(db,mcx,sql);
					
				} catch (SQLiteException e) {
					Log.e(tag,"insert apkpos_orders_new error:" + e.getMessage());
				}
				
				Intent intent = new Intent();
				Bundle bundle = new Bundle();
				bundle.putString("orderid", orderid);
				bundle.putString("room_name", room_name+vipflag);
				bundle.putString("room_id", room_id);
				bundle.putInt("people_num", 1);
				bundle.putInt("price_type", price_type);
				intent.putExtras(bundle);
				intent.setClass(RoomoperateActivity.this, DishordersActivity.class);
				startActivity(intent);
				overridePendingTransition(R.anim.zoomin, R.anim.zoomout);
				finish();
				
			}
		});
		

	}
	
	public void execSQL (SQLiteDatabase db, Context cx,String sql) {
		if (db == null || !db.isOpen()) {
			db = DBHelper.getDatabase(cx,1);
		}		
		db.execSQL(sql);		
	}
	public Cursor rawQuery(SQLiteDatabase db, Context cx,String sql) {
		if (db == null || !db.isOpen()) {
			db = DBHelper.getDatabase(cx,0);
		}	
		
		Cursor cursor=db.rawQuery(sql, null);
		return cursor;
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		System.gc(); // 回收系统资源
		super.onDestroy();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

}
